﻿mockData = [
            { Id: "ServiceGetAllCustomers", Label: "Service: GetAllCustomers", TotalTime: 0.1, UserTime: 0.11, SystemTime: 0.12, RealTime: 0.13, Timestamp: new Date(2010, 1, 1) },
            { Id: "ServiceGetAllOrders", Label: "Service: GetAllOrders", TotalTime: 0.3, Timestamp: new Date(2010, 1, 1) },
            { Id: "ServiceGetAllOrders", Label: "Service: GetAllOrders", TotalTime: 0.5, Timestamp: new Date(2010, 1, 2) },
            //{ Label: "Service-GetAllOrders", TotalTime: 0.44, Timestamp: new Date(2010, 1, 6) },
            { Id: "ServiceFindCustomer", Label: "Service: FindCustomer", TotalTime: 1.1, Timestamp: new Date(2010, 1, 1) },
            { Id: "ServiceFindCustomer", Label: "Service: FindCustomer", TotalTime: 1.9, Timestamp: new Date(2010, 1, 2) }
        ];

var setupScenario = function() {
    $("#content").empty();

    $ajax = {
        getJSON: function(url, callback) {
            callback(mockData);
        }
    }
}

describe("when initialized and div content container doesn't exist", function() {
    it("should generate div content container", function() {
        setupScenario();
        initializeReport();

        expect($("#content").length).toBe(1);
    });
});

describe("when initialized", function() {
    beforeEach(function() {
        setupScenario();
        initializeReport();
    });

    it("should grab data from repository", function() {
        expect(data).not.toBeNull();
    });

    it("should have initialized global $ajax variable", function() {
        expect($ajax).not.toBeNull();
    });

    it("should insert a div container for each timeseries", function() {
        timeseries = transformToTimeseries(data);
        for (i in timeseries) {
            ts = timeseries[i];
            expect($("#" + ts.name).length).toBe(1);
        }
    });
});

describe("when transform data to timeseries", function() {
    beforeEach(function() {
        setupScenario();
        initializeReport();
    });

    it("should return a resultset", function() {
        var timeseries = transformToTimeseries(data);

        expect(timeseries).not.toBeNull();
        expect(timeseries.length).not.toBe(0);
    });

    it("should group timeseries by label name", function() {
        var timeseries = transformToTimeseries(data);

        expect(timeseries.length).toBe(3);
        expect(timeseries[0].name).toBe("ServiceGetAllCustomers");
        expect(timeseries[1].name).toBe("ServiceGetAllOrders");
        expect(timeseries[2].name).toBe("ServiceFindCustomer");
    });

    it("should generate values for each timeseries", function() {
        var timeseries = transformToTimeseries(data);

        //Documenting for my self how jasmine logging API works.
        jasmine.log(timeseries);

        getAllCustomersTs = findByNameIn(timeseries, "ServiceGetAllCustomers");
        getAllOrdersTs = findByNameIn(timeseries, "ServiceGetAllOrders");
        findCustomerTs = findByNameIn(timeseries, "ServiceFindCustomer");

        expect(getAllCustomersTs.values.length).toBe(1);
        expect(getAllCustomersTs.totalTime.length).toBe(1);
        expect(getAllCustomersTs.userTime.length).toBe(1);
        expect(getAllCustomersTs.systemTime.length).toBe(1);
        expect(getAllCustomersTs.realTime.length).toBe(1);
        expect(getAllCustomersTs.values[0].totaltime).toBe(0.1);
        expect(getAllCustomersTs.values[0].date).not.toBeNull();

        expect(getAllOrdersTs.values.length).toBe(2);
        expect(getAllOrdersTs.totalTime.length).toBe(2);
        expect(getAllOrdersTs.userTime.length).toBe(2);
        expect(getAllOrdersTs.systemTime.length).toBe(2);
        expect(getAllOrdersTs.realTime.length).toBe(2);
        expect(getAllOrdersTs.values[0].date).not.toBeNull();
        expect(getAllOrdersTs.values[0].totaltime).toBe(0.3);
        expect(getAllOrdersTs.values[1].date).not.toBeNull();
        expect(getAllOrdersTs.values[1].totaltime).toBe(0.5);

        expect(findCustomerTs.values.length).toBe(2);
        expect(findCustomerTs.totalTime.length).toBe(2);
        expect(findCustomerTs.userTime.length).toBe(2);
        expect(findCustomerTs.systemTime.length).toBe(2);
        expect(findCustomerTs.realTime.length).toBe(2);
        expect(findCustomerTs.values[0].totaltime).toBe(1.1);
        expect(findCustomerTs.values[1].totaltime).toBe(1.9);
    });
});